-- The MIT License (MIT)
-- Copyright (c) 2016 Yanghe Sun

--luacheck: no global
--luacheck: no unused args

local compiler = require("compiler")()
local luaunit  = require("luaunit")
local runner   = require("runner")

Test_runner = {}

function Test_runner:testInvaildConfig()
    luaunit.assertErrorMsgContains("bad", runner.Run, "stupid");
    luaunit.assertErrorMsgContains("bad", runner.Run, {
        dump_mask     = 0,
        max_cpu_time  = runner._UNLIMITED,
        max_real_time = runner._UNLIMITED,
        max_memory    = runner._UNLIMITED,
        args          = {}
    });
end

function Test_runner:testMemoryLimit()
    local info = compiler:Compile("./testcases/memory1.c");
    assert(info.success);

    luaunit.assertEquals(runner.Run({
        max_cpu_time  = runner._UNLIMITED,
        max_real_time = runner._UNLIMITED,
        max_memory    = 10 * 1024,
        exe_path      = info.exe_path,
        args          = { info.exe_path }

    }).flag, runner._RESULT_SUCCESS);

    info = compiler:Compile("./testcases/memory2.c");
    assert(info.success);

    luaunit.assertEquals(runner.Run({
        max_cpu_time  = runner._UNLIMITED,
        max_real_time = runner._UNLIMITED,
        max_memory    = 10 * 1024,
        exe_path      = info.exe_path,
        args          = { info.exe_path }

    }).flag, runner._RESULT_MEMORY_LIMIT_EXCEEDED);
end

function Test_runner:testTimeLimit()
    local info = compiler:Compile("./testcases/while1.c");
    assert(info.success);

    luaunit.assertEquals(runner.Run({
        max_cpu_time  = 500,
        max_real_time = 500,
        max_memory    = runner._UNLIMITED,
        exe_path      = info.exe_path,
        args          = { info.exe_path }

    }).flag, runner._RESULT_TIME_LIMIT_EXCEEDED);
end

function Test_runner:testRuntimeError()
    local info = compiler:Compile("./testcases/re1.c");
    assert(info.success);

    luaunit.assertEquals(runner.Run({
        max_cpu_time  = runner._UNLIMITED,
        max_real_time = runner._UNLIMITED,
        max_memory    = runner._UNLIMITED,
        exe_path      = info.exe_path,
        args          = { info.exe_path }

    }).flag, runner._RESULT_RUNTIME_ERROR);

    info = compiler:Compile("./testcases/re2.c");
    assert(info.success);

    luaunit.assertEquals(runner.Run({
        max_cpu_time  = runner._UNLIMITED,
        max_real_time = runner._UNLIMITED,
        max_memory    = runner._UNLIMITED,
        exe_path      = info.exe_path,
        args          = { info.exe_path }

    }).flag, runner._RESULT_RUNTIME_ERROR);
end
